/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TempCart;
import DBManager.DBConnection;
import java.sql.*;
import DBManager.DBConnection;

/**
 *
 * @author maricone
 */
public class CartManager {
    
       public boolean addCart(Cart cart)
    {
        boolean result = false;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("insert into tempcart(customerid,productid)"+
                "values(?,?)");
int i=1;
        
            pstmt.setInt(i++, cart.getCustomerid());
                 pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
            pstmt.executeUpdate();
            
            result = true;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
        public int checkCart(Cart cart)
    {
        int result = 0;
        ResultSet rs=null;
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("select productid from tempcart where productid=?");
            
int i=1;
        
           // pstmt.setInt(i++, cart.getCustomerid());
                 pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
         rs=pstmt.executeQuery();
         while(rs.next()){
             rs.getInt("productid");
             
         }
            
            result =rs.getInt("productid");
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
       
        public boolean updateCart(Cart cart)
    {
        boolean result = false;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("UPDATE tempcart SET quantity=? WHERE cartid=?");
int i=1;
            pstmt.setInt(i++, cart.getQuanity());
            pstmt.setInt(i++, cart.getCartid());
                 //pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
            pstmt.executeUpdate();
            
            result = true;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
         public boolean deleteCart(Cart cart)
    {
        boolean result = false;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("delete from tempcart where cartid=?");
int i=1;
            //pstmt.setInt(i++, cart.getQuanity());
            pstmt.setInt(i++, cart.getCartid());
                 //pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
            pstmt.executeUpdate();
            
            result = true;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
       
}
